import random

from plotting.drawer import plot_gantt_chart, draw_precedence_relations
from data_parsers.parser_fjsp import parse_fjsp
from scheduling_environment.jobShop import JobShop

from solution_methods.helper_functions import load_job_shop_env, load_parameters
from solution_methods.dispatching_rules.run_dispatching_rules import run_dispatching_rules
from solution_methods.GA.src.initialization import initialize_run
from solution_methods.GA.run_GA import run_GA
from solution_methods.MILP.run_MILP import run_MILP
from solution_methods.FJSP_DRL.run_FJSP_DRL import run_FJSP_DRL

#
# jobShopEnv = JobShop()
# jobShopEnv = parse_fjsp(jobShopEnv, '/fjsp/brandimarte/MK01.fjs')
#
# jobShopEnv = load_job_shop_env('/fjsp/brandimarte/MK01.fjs')
#
# draw_precedence_relations(jobShopEnv)
#
#
# # Display the Job Shop Environment information
# print('作业车间环境：/Job Shop Environment:')
# print(jobShopEnv)
#
#
# # Display the list of jobs in the environment
# print('作业如下：/With the following Jobs:')
# print(jobShopEnv.jobs)
#
#
# # Display the operations of the first job (job_id=0)
# print('Where the first job (job_id=0) has the following operations:')
# job = jobShopEnv.get_job(job_id=0)
# print(job.operations)

jobShopEnv = load_job_shop_env('/fjsp/brandimarte/MK04.fjs')
jobShopEnv.update_operations_available_for_scheduling()
while len(jobShopEnv.scheduled_operations) < jobShopEnv.nr_of_operations:
    operation = random.choice(jobShopEnv.operations_available_for_scheduling)
    machine_id = random.choice(list(operation.processing_times.keys()))
    duration = operation.processing_times[machine_id]
    jobShopEnv.schedule_operation_on_machine(operation, machine_id, duration)
    jobShopEnv.update_operations_available_for_scheduling()

plot_gantt_chart(jobShopEnv)


parameters = load_parameters("configs/GA.toml")
jobShopEnv = load_job_shop_env(parameters['instance'].get('problem_instance'))

population, toolbox, stats, hof = initialize_run(jobShopEnv, **parameters)
makespan, jobShopEnv = run_GA(jobShopEnv, population, toolbox, stats, hof, **parameters)

plot_gantt_chart(jobShopEnv)